<?php

class PagesController extends Controller
{
  public $layout='//layouts/admin';

  public function filters()
  {
    return array(
      'accessControl',
    );
  }

  public function accessRules()
  {
    return array(
      array('allow',
        'actions'=>array('index','view'),
        'users'=>array('*'),
      ),
      array('allow',
        'actions'=>array('update', 'list', 'create', 'updateStatus'),
        'users'=>array('@'),
      ),
      array('deny',
        'users'=>array('*'),
      ),
    );
  }

  public function actionView($data)
  {
    $this->layout = '//layouts/main';
    $model = Pages::model()->find(array(
      'condition'=>'slug=:slug',
      'params'=>array(':slug' => $data),
    ));

    $this->metaKeyword = $model->name_en. ' : '.$this->metaKeyword;
    $this->metaDescription = strip_tags($model->description);

    $this->render('view',array(
      'model'=>$model
    ));
  }

  public function actionCreate()
  {
    $model=new Pages;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Pages']))
    {
      $model->attributes=$_POST['Pages'];
      if($model->save())
        $this->redirect(array('list'));
    }

    $this->render('create',array(
      'model'=>$model,
    ));
  }

  public function actionUpdateStatus($id)
  {
    $model = Pages::model()->find(array(
        'condition'=>'id=:id',
        'params'=>array(':id' => $id),
      ));

    $model->enable=($model->enable==true) ? 0 : 1;
    $model->save();
    $this->redirect(array('list'));
  }  
  
  public function actionUpdate($id)
  {
    $model = Pages::model()->find(array(
        'condition'=>'id=:id',
        'params'=>array(':id' => $id),
      ));

    if(isset($_POST['Pages']))
    {
      $model->attributes=$_POST['Pages'];
      if($model->save())
      {
        $this->redirect(array('list'));        
      }
    }

    $this->render('update',array(
      'model'=>$model,
    ));
  }

  public function actionDelete($id)
  {
    if(Yii::app()->request->isPostRequest)
    {
      // we only allow deletion via POST request
      $this->loadModel($id)->delete();

      // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
      if(!isset($_GET['ajax']))
        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }
    else
      throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
  }

  public function actionList()
  {
    $dataProvider=new CActiveDataProvider('Pages');
    $this->render('list',array(
      'dataProvider'=>$dataProvider,
    ));
  }

  public function actionAdmin()
  {
    $model=new Page('search');
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['Page']))
      $model->attributes=$_GET['Page'];

    $this->render('admin',array(
      'model'=>$model,
    ));
  }

  public function loadModel($id)
  {
    $model=Pages::model()->findByPk($id);
    if($model===null)
      throw new CHttpException(404,'The requested page does not exist.');
    return $model;
  }

  protected function performAjaxValidation($model)
  {
    if(isset($_POST['ajax']) && $_POST['ajax']==='page-form')
    {
      echo CActiveForm::validate($model);
      Yii::app()->end();
    }
  }
}
